package com.shop.seller.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSON;
import com.shop.domain.CustomGoods;
import com.shop.domain.CustomGoodsdesc;
import com.shop.domain.CustomItem;
import com.shop.domain.CustomSeller;
import com.shop.domain.CustomTypeTemplate;
import com.shop.domain.GoodsQueryVo;
import com.shop.domain.PageResult;
import com.shop.model.Goods;
import com.shop.model.Goods_desc;
import com.shop.model.Item;
import com.shop.model.ItemCat;
import com.shop.seller.service.BrandService;
import com.shop.seller.service.GoodsService;
import com.shop.seller.service.ItemCatService;
import com.shop.seller.service.SpecificationService;
import com.shop.seller.service.TypeTemplateService;
import com.shop.utils.UploadFile;

@Controller
@RequestMapping("/goods")
public class GoodsAction {

	
	@Resource
	private GoodsService goodsService;
	@Resource
	private BrandService brandService;
	@Resource
	private ItemCatService itemCatService;
	@Resource
	private SpecificationService specificationService;
	@Resource
	private TypeTemplateService typeTemplateService;
	
	/*************商品管理部分******************/
	
     /**
      * 查找商品
      * @param goods
      * @param pageNum
      * @param pageSize
      * @param model
      * @param tag
      * @param response
      * @return
      * @throws Exception
      */
	@RequestMapping("/findGoods")
	public String find(Goods goods, Integer pageNum, Integer pageSize, Model model,String tag, HttpServletResponse response,HttpServletRequest request)
			throws Exception {
		pageNum = (pageNum == null ? 1 : pageNum); // 默认是第一页的
		pageSize = (pageSize == null ? 5 : pageSize);//默认一页显示5条数据
		response.setContentType("text/html,charset=utf-8");
		response.setCharacterEncoding("utf-8");
		//从session取出商家信息
		CustomSeller customSeller=(CustomSeller) request.getSession().getAttribute("customSeller");
		//把商家id值赋给goods用于查找属于商家自己的商品
		goods.setSellerId(customSeller.getSellerId());
		PageResult pageResult = goodsService.findPage(goods, pageNum, pageSize);
		model.addAttribute("pageResult", pageResult);
		model.addAttribute("pageNo", pageNum);
		
		
		if("market".equals(tag)){
		model.addAttribute("goodsStatus",goods.getIsMarketable());
		}
		if("status".equals(tag)){
			model.addAttribute("goodsStatus",goods.getAuditStatus());	
		}
		return "seller/admin/goods";
	}

     /**
      * 显示商品
      * @return
      * @throws IOException
      */
	@RequestMapping("/goodsShow")
	public String goodShow() throws IOException {
		return "redirect:findGoods.action";
	}

	/**
	 * 删除商品
	 * @param goodsId
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping("/deleteGoods")
	public void deleteGood(String goodsId, HttpServletResponse response) throws Exception {
		PrintWriter out = response.getWriter();
		String[] ids = goodsId.split(",");
		boolean flag = false;

		for (String id : ids) {

			flag = goodsService.deleteGoods(Long.valueOf(id));

		}

		out.print(flag);

	}
/**
 * 更新商品
 * @param goodsId
 * @param tag
 * @param response
 * @throws Exception
 */
	@RequestMapping("/update")
	public void update(String goodsId, String tag, HttpServletResponse response) throws Exception {
		PrintWriter out = response.getWriter();
		String[] ids = goodsId.split(",");
		Goods good = null;
		boolean flag = false;
		if ("putaway".equals(tag)) {
			for (String id : ids) {

				good = goodsService.findGoodsById(Long.valueOf(id));
				good.setIsMarketable("5"); //是已上架
				flag = goodsService.updateGoods(good);
			}
		} else if ("takeaway".equals(tag)) {
			for (String id : ids) {

				good = goodsService.findGoodsById(Long.valueOf(id));
				good.setIsMarketable("4");//未上架
				flag = goodsService.updateGoods(good);
			}

		} else {
			for (String id : ids) {

				good = goodsService.findGoodsById(Long.valueOf(id));
				good.setAuditStatus("1");
				flag = goodsService.updateGoods(good);
			}

		}

		out.print(flag);

	}
	
	/*************商品新增部分******************/
	
	/**
	 * 查找品牌
	 * @param request
	 * @param response
	 * @param parentid
	 */
	@RequestMapping("/findBrands")
	@ResponseBody
	public void getBrands(HttpServletRequest request,HttpServletResponse response,String brandId){

		try {
			//解决Post提交的中文乱码问题
			request.setCharacterEncoding("utf-8");	
			//解决响应的中文乱码问题
			response.setContentType("text/html;charset=utf-8");
			String brands=brandService.findBrands(brandId);
            PrintWriter out=response.getWriter();
			
			out.println(brands);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	/**
	 * 查找全部分类
	 * @param request
	 * @param response
	 * @param parentid
	 */
	@RequestMapping("/findItemCats")
	@ResponseBody
	public void getItemCatByParentId(HttpServletRequest request,HttpServletResponse response,Long id){
		String itemCats = null;
		
		try {
			//解决Post提交的中文乱码问题
			request.setCharacterEncoding("utf-8");	
			//解决响应的中文乱码问题
			response.setContentType("text/html;charset=utf-8");
			itemCats = itemCatService.findItemCatByParentId(id);
            PrintWriter out=response.getWriter();
			out.println(itemCats);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	/**
	 * 查找单个分类
	 * @param request
	 * @param response
	 * @param parentid
	 */
	@RequestMapping("/findItemCat")
	@ResponseBody
	public void getItemCatById(HttpServletRequest request,HttpServletResponse response,long id){
		ItemCat itemCat = null;
		try {
			//解决Post提交的中文乱码问题
			request.setCharacterEncoding("utf-8");	
			//解决响应的中文乱码问题
			response.setContentType("text/html;charset=utf-8");
			itemCat = itemCatService.findItemCatById(id);
            PrintWriter out=response.getWriter();
			String json=JSON.toJSON(itemCat).toString();
			out.println(json);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	/**
	 * 查找规格选项
	 * @param request
	 * @param response
	 * @param id
	 */
	@RequestMapping("/findSpecificationOptions")
	@ResponseBody
	public void findSpecificationOptions(HttpServletRequest request,HttpServletResponse response,long id){
		String json="";
		PrintWriter out=null;
		try {
			//解决响应的中文乱码问题
			response.setContentType("text/html;charset=utf-8");
			//解决Post提交的中文乱码问题
			request.setCharacterEncoding("utf-8");
			out=response.getWriter();
			List<Map> options=specificationService.findSpecList(id);
			json=JSON.toJSON(options).toString();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
		out.println(json);
	}
	
	/**
	 * 查找模板
	 * @param request
	 * @param response
	 * @param parentid
	 */
	@RequestMapping("/findTypeTemplate")
	@ResponseBody
	public void getTypeTemplateById(HttpServletRequest request,HttpServletResponse response,long id){
		
		CustomTypeTemplate typeTemplate = null;
		 PrintWriter out=null;
		try {
			//解决Post提交的中文乱码问题
			request.setCharacterEncoding("utf-8");	
			//解决响应的中文乱码问题
			response.setContentType("text/html;charset=utf-8");
			typeTemplate = typeTemplateService.findTypeTemplate(id);
			out=response.getWriter();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String json=JSON.toJSON(typeTemplate).toString();
		out.println(json);
	}
	
	/**
	 * 完成商品添加
	 * @param request
	 * @param response
	 * @param goods
	 */
	@RequestMapping("/addGoods")
	@ResponseBody
	public void addGoods(HttpServletRequest request,HttpServletResponse response,@RequestBody GoodsQueryVo goods){		
		PrintWriter out=null;
		try {
			//解决Post提交的中文乱码问题
			request.setCharacterEncoding("utf-8");	
			//解决响应的中文乱码问题
			response.setContentType("text/html;charset=utf-8");
			out=response.getWriter();
			//获取商家id赋给商品
	     	CustomSeller seller=(CustomSeller)request.getSession().getAttribute("customSeller");
	     	goods.getCustomGoods().setSellerId(seller.getSellerId());
			goodsService.addGoods(goods);			
		    out.println(true);						
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
		 out.println(false);	
	}
	

}
